package com.accenture.portal.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.accenture.portal.model.BusinessLog;
import com.accenture.portal.service.BusinessLogService;

@Controller
public class BusinessLogController {
	@Autowired
	BusinessLogService businessLogService;
	
	@RequestMapping("searchBusinessLogInfo.action")
	public String searchBusinessLogInfo(@RequestParam(value="cur",required=false)Integer cur,HttpServletRequest request,
			HttpServletResponse response,Model model) throws ParseException{
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		String operate_account = request.getParameter("operate_account");
		String model_name = request.getParameter("model_name");
		String status = request.getParameter("status");
		String begainDate = request.getParameter("begainDate");
		String endDate = request.getParameter("endDate");
		
		SimpleDateFormat transac = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
		if(begainDate != null && begainDate.length() > 0){
			Date begain = transac.parse(begainDate);
			map.put("begain", begain);
		}
		if(endDate != null && endDate.length() > 0){
			Date end = transac.parse(endDate);
			map.put("end", end);
		}
		
		map.put("operate_account", operate_account);
		map.put("model_name", model_name);
		map.put("status", status);
		
		//分页处理
		
		map.put("pageSize", 0);
		int total = businessLogService.SBBDAAAMAS(map).size();//总条数
		int pageSize = 10;//每页显示10条
		int pageNumber = total/pageSize; //总页数
		
		
		if(total % pageSize != 0){
			pageNumber++;
		}
		
		if(pageNumber == 0){
			pageNumber = 1;
		}
		
		if(cur == null  || cur < 1){
			cur = 1;
		}else if(cur > pageNumber){
			cur = pageNumber;
		}
		
		int begainData = (cur - 1) * pageSize;
		
		//将当前页开始的第一条数据的序号和每页的条数放到MAP中，传给SQL语句
		map.put("begainData", begainData);
		map.put("pageSize", pageSize);
		
		List<BusinessLog> businessLogInfo = businessLogService.SBBDAAAMAS(map);
		
		model.addAttribute("businessLogInfo", businessLogInfo);
		
		request.getSession().setAttribute("cur", cur);
		request.getSession().setAttribute("pageNumber", pageNumber);
		
		request.getSession().setAttribute("operate_account", operate_account);
		request.getSession().setAttribute("model_name", model_name);
		request.getSession().setAttribute("status", status);
		request.getSession().setAttribute("begainDate", begainDate);
		request.getSession().setAttribute("endDate", endDate);
		
		return "businessLog_search";
	}
}
